package com.sharer.last.utils;

import com.sharer.last.model.po.UserPO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * 文件注释地方
 *
 * @ClassName SecurityHelper
 * @Author WangJin
 * @Date 2023/06/14/15:16
 * @Description
 * @Version 1.0
 */
@Component
public class SecurityHelper {

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    public static UserPO getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserPO         user           = (UserPO) authentication.getPrincipal();
        if (user == null) {
            return UserPO.builder().username("未知访客").id(-1L).build();
        }
        user.setPassword(null);
        return user;
    }

    /**
     * 获取当前登录用户id
     *
     * @return
     */
    public static Long getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserPO         user           = (UserPO) authentication.getPrincipal();
        if (user == null) {
            return -1L;
        }
        return user.getId();
    }

    /**
     * 获取当前登录用户id
     *L
     * @return
     */
    public static Long getUserIdWithException() {
        return 1L;
    }
}
